package com.boco.wmworkflow.common.xmlmodel;

import java.util.*;
import com.boco.common.util.xml.AbstractXmlModel;
import com.boco.webmaster.common.xsdmap.workflow.wfcfg.*;

public class WfCfgXmlModel extends AbstractXmlModel{
    private final Map wfCfgTable = new HashMap();
    private static WfCfgXmlModel instance;

    public static WfCfgXmlModel createInstance(String xmlFileName) throws Exception{
        if(instance == null){
            instance = new WfCfgXmlModel(xmlFileName);
        }
        return instance;
    }

    public static WfCfgXmlModel getInstance(){
        return instance;
    }

    private WfCfgXmlModel(String xmlFileName) throws Exception{
        super("com.boco.webmaster.common.xsdmap.workflow.wfcfg", xmlFileName);
        WfCfgModelType rootModel = (WfCfgModelType)super.getRootModel();
        List wfCfgList = rootModel.getWfCfgList();
        for(int i=0; i<wfCfgList.size(); i++){
            WfCfgType wfCfg = (WfCfgType) wfCfgList.get(i);
            wfCfgTable.put(wfCfg.getProcessId(), wfCfg);
        }
    }

    public WfCfgType getWorkFlowCfg(String processId){
        return (WfCfgType)wfCfgTable.get(processId);
    }

    public WfCfgType[] getAllWfCfgs(){
        WfCfgModelType rootModel = (WfCfgModelType)super.getRootModel();
        List wfCfgList = rootModel.getWfCfgList();
        WfCfgType[] wfCfgs = new WfCfgType[wfCfgList.size()];
        wfCfgList.toArray(wfCfgs);
        return wfCfgs;
    }

    public Map getWorkflowParas(String processId){
        Map paras = new HashMap();
        WfCfgType wfCfg = WfCfgXmlModel.getInstance().getWorkFlowCfg(processId);
        List paraList = wfCfg.getParaList();
        for (int i = 0; i < paraList.size(); i++) {
            NameValueType nv = (NameValueType) paraList.get(i);
            paras.put(nv.getName(), nv.getValue());
        }
        return paras;
    }
}
